package com.leo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.TextFieldTreeCell;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 * @ClassName TreeViewDemo
 * @Description
 * @Author wangss
 * @date 2020.06.14 22:53
 * @Version 1.0
 */
public class TreeViewDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane an = new AnchorPane();
        an.setStyle("-fx-background-color: #f4fffe");

        TreeView<String> treeView = new TreeView<>();
        treeView.setPrefHeight(200d);

        TreeItem<String> root = new TreeItem<>("中国");

        TreeItem<String> hlj = new TreeItem<>("黑龙江省");
        TreeItem<String> cityhlj = new TreeItem<>("黑龙江");
        TreeItem<String> cityheb = new TreeItem<>("哈尔滨");
        TreeItem<String> citydq = new TreeItem<>("大庆");

        TreeItem<String> hn = new TreeItem<>("河南省");
        TreeItem<String> citypy = new TreeItem<>("濮阳");
        TreeItem<String> cityzz = new TreeItem<>("郑州");
        TreeItem<String> citykf = new TreeItem<>("开封");

        TreeItem<String> js = new TreeItem<>("江苏省");
        TreeItem<String> citynj = new TreeItem<>("南京");
        TreeItem<String> cityjs = new TreeItem<>("江苏");

        treeView.setRoot(root);
        root.getChildren().addAll(hlj, hn, js);
        hlj.getChildren().addAll(cityhlj, cityheb, citydq);
        hn.getChildren().addAll(citypy, cityzz, citykf);
        js.getChildren().addAll(citynj, cityjs);

        // 自动展开
        root.setExpanded(true);
        hlj.setExpanded(true);
        hn.setExpanded(true);
        js.setExpanded(true);

        // 滚动到
        treeView.scrollTo(10);

        treeView.getSelectionModel().select(10);

        // 是否显示根节点
        treeView.setShowRoot(true);

        AnchorPane.setTopAnchor(treeView, 50d);
        AnchorPane.setLeftAnchor(treeView, 100d);
        an.getChildren().add(treeView);

        Scene scene = new Scene(an);
        primaryStage.setScene(scene);
        primaryStage.setTitle("TreeView");
        primaryStage.setWidth(800d);
        primaryStage.setHeight(800d);
        primaryStage.show();

        // 多选
//        treeView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

        treeView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {

        });

        treeView.getSelectionModel().selectedIndexProperty().addListener((observable, oldValue, newValue) -> {

        });

        treeView.requestFocus();

        // CheckBoxTreeCell, ChoiceBoxTreeCell, ComboBoxTreeCell, TextFieldTreeCell
        treeView.setEditable(true);
        treeView.setCellFactory(TextFieldTreeCell.forTreeView());

//        root.setGraphic();


    }
}
